package com.czxy.controller;

import com.czxy.service.CartService;
import com.czxy.vo.BaseResult;
import com.czxy.vo.CartRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 张青卯
 */
@RestController
@RequestMapping
public class CartController {

    private final CartService cartService;

    private final HttpServletRequest request;

    @Autowired
    public CartController(CartService cartService, HttpServletRequest request) {
        this.cartService = cartService;
        this.request = request;
    }

    @PostMapping("/carts")
    public ResponseEntity<BaseResult> addToCart(@RequestBody CartRequest cartRequest){
        cartService.addToCart(cartRequest,request);
        BaseResult result = new BaseResult(0, "成功", null);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/carts")
    public ResponseEntity<Object> getShoppingCart(){
        List<CartRequest> list = cartService.getShoppingCart(request);
        BaseResult result = new BaseResult(0,"成功",list);
        return ResponseEntity.ok(result);
    }

    @PutMapping("/carts/{skuid}")
    public ResponseEntity<Object> modifyShoppingCart(@PathVariable("skuid") Integer skuid,CartRequest cartRequest){
        cartService.modifyShoppingCart(skuid,cartRequest,request);
        BaseResult result = new BaseResult(0,"成功",null);
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/carts/{id}")
    public ResponseEntity<Object> deleteShoppingCart(@PathVariable("id") Integer id){
        cartService.deleteShoppingCart(id,request);
        BaseResult result = new BaseResult(0,"成功",null);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/carts/addLoginCart")
    ResponseEntity<BaseResult> addLoginCart(@RequestParam("userId") Integer userId,@RequestParam("carts") String carts){
        cartService.addLoginCart(userId,carts);
        BaseResult result = new BaseResult(0, "成功");
        return ResponseEntity.ok(result);
    }

}
